﻿using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    /// <summary>
    /// 报修维护控制器
    /// </summary>
    public class MaintainController : Controller
    {
        /// <summary>
        /// 报修维护页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GuaranteeList()
        {
            IRepairManager manager = new RepairManager();
            var data = manager.GetGuaranteeListData();
            ViewBag.AllCount = data.AllCount;
            ViewBag.AllSuccess = data.AllSuccessCount;
            ViewBag.thisCount = data.thisCount;
            ViewBag.thisSuccessCount = data.thisSuccessCount;
            return View();
        }
        /// <summary>
        /// 报修维护页面table表的数据获取接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="RepairTitle">报修名称筛选</param>
        /// <param name="RepairContent">报修内容筛选</param>
        /// <param name="UserName">报修人筛选</param>
        /// <returns></returns>
        public JsonResult GetUserGuaranteeList(int limit, int page, string RepairTitle, string RepairContent, string UserName)
        {
            IRepairManager manager = new RepairManager();
            int datalet;
            var data = manager.GetUserGuaranteeLists(limit, page, out datalet, RepairTitle, RepairContent, UserName);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 报修维护页面table表单确定按钮数据请求接口 确认报修信息已完成
        /// </summary>
        /// <param name="id">报修ID</param>
        /// <returns></returns>
        public JsonResult SuccessOneData(int id, bool repairState)
        {
            IRepairManager manager = new RepairManager();
            manager.SuccessOneDataById(id, repairState);
            return Json(new { code = 0, msg = "确定成功" }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult WriteReasonToNoOk(int id)
        {
            ViewBag.id = id;
            return View();
        }
        [HttpPost]
        public JsonResult WriteReasonToNoOk(GetUserGuaranteeListDto dto)
        {
            IRepairManager manager = new RepairManager();
            manager.SuccessOneDataById(dto.Id, false, dto.RepairContent);
            return Json(new { code = 0, msg = "数据录入成功" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 受理按钮请求接口
        /// </summary>
        /// <param name="id"></param>
        /// <param name="repairState"></param>
        /// <returns></returns>
        public JsonResult IsAcceptance(int id, bool repairResult)
        {
            IRepairManager manager = new RepairManager();
            manager.IsAcceptanceById(id, repairResult, "");
            return Json(new { code = 0, msg = "受理成功" }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult WriteReason(int id)
        {
            ViewBag.id = id;
            return View();
        }

        [HttpPost]
        public JsonResult WriteReason(GetUserGuaranteeListDto dto)
        {
            IRepairManager manager = new RepairManager();
            manager.IsAcceptanceById(dto.Id, false, dto.RepairContent);
            return Json(new { code = 0, msg = "拒受理成功" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加报修页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddUserGuarantee()
        {
            ViewBag.userPer = Session["DepartMentId"].ToString();
            return View();
        }
        /// <summary>
        /// 添加报修页面的tableselect下拉框数据请求接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="HouseholdName">住户名筛选</param>
        /// <returns></returns>
        public JsonResult GetHouseholdName(int limit, int page, string HouseholdName)
        {
            IRepairManager manager = new RepairManager();
            int datalet;
            var data = manager.GetHouseholdNameLists(limit, page, HouseholdName, out datalet);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加报修页面数据请求接口，添加一条报修数据
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUserGuarantee(GetUserGuaranteeListDto listDto)
        {
            IRepairManager manager = new RepairManager();
            if (listDto.UserName == "住户上报")
            {
                listDto.UserName = Session["userid"].ToString();
            }
            manager.AddOneUserGuarantee(listDto);
            return Json(new { code = 0, msg = "添加成功" });
        }
    }
}